//-----------------------------------------------------------------------
// <copyright file="a_SurveyDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_SurveyDAOBase
    {
        #region Common methods
        public virtual a_Survey Createa_SurveyFromReader(IDataReader reader)
        {
            a_Survey item = new a_Survey();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("QID"))) item.QID = (int)reader["QID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Question"))) item.Question = (string)reader["Question"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (byte)reader["Status"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Survey methods
            
        public virtual void Createa_Survey(a_Survey a_Survey)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_ins");
                
                database.AddInParameter(dbCommand, "@Question", DbType.String, a_Survey.Question);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Survey.Status);
                database.AddOutParameter(dbCommand, "@QID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Survey.QID = (int)database.GetParameterValue(dbCommand, "@QID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Survey methods
        
        public virtual void Updatea_Survey(a_Survey a_Survey)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_upd");            
                
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, a_Survey.QID);
                database.AddInParameter(dbCommand, "@Question", DbType.String, a_Survey.Question);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Survey.Status);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Survey methods
        public virtual void Deletea_Survey(int qid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_del");
                
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, qid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Survey methods
        
        public virtual a_Survey Geta_Survey(int qid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_sel");
                
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, qid);
                
                a_Survey a_Survey =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Survey = Createa_SurveyFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Survey;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_SurveyList methods
        public virtual a_SurveyCollection Geta_SurveyList(a_SurveyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_SurveyCollection a_SurveyCollection = new a_SurveyCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Survey a_Survey = Createa_SurveyFromReader(reader);
                        a_SurveyCollection.Add(a_Survey);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SurveyCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_SurveyCollection Geta_SurveyList(a_SurveyColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_SurveyList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_SurveyList_alphabet_stt methods
        public virtual a_SurveyCollection Geta_SurveyList_alphabet_stt(int stt,string mychar, a_SurveyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_selList_alphabet_stt");
                database.AddInParameter(dbCommand, "@Stt", DbType.Int32, stt);
                database.AddInParameter(dbCommand, "@Mychar", DbType.AnsiString,mychar);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_SurveyCollection a_SurveyCollection = new a_SurveyCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Survey a_Survey = Createa_SurveyFromReader(reader);
                        a_SurveyCollection.Add(a_Survey);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SurveyCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_SurveyCollection Geta_SurveyList_alphabet_stt(int stt, string mychar, a_SurveyColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_SurveyList_alphabet_stt(stt, mychar, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        #region Geta_SurveyList_alphabet_stt methods
        public virtual a_SurveyCollection Geta_SurveyList_stt(int stt,a_SurveyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_selList_stt");
                database.AddInParameter(dbCommand, "@Stt", DbType.Int32, stt);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_SurveyCollection a_SurveyCollection = new a_SurveyCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Survey a_Survey = Createa_SurveyFromReader(reader);
                        a_SurveyCollection.Add(a_Survey);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SurveyCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_SurveyCollection Geta_SurveyList_stt(int stt,a_SurveyColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_SurveyList_stt(stt, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}